﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo
{
    /// <summary>
    /// IFeature扩展方法
    /// </summary>
    public static class IFeatureExtend
    {
        /// <summary>
        /// 将Feature转换成Model对象，并更新其属性值，并保存
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public static bool SaveToModel(this DotSpatial.Data.IFeature target)
        {
            if (target.DataRow.Table != null && target.DataRow.Table.Columns.Contains("DataKey"))
            {
                Model.PointModel pm = Model.PointModel.FindByDatakey(target.DataRow["Datakey"].ToString());
                if (pm != null)
                {
                    Type t = pm.GetType();
                    System.Reflection.PropertyInfo[] infos = t.GetProperties();
                    if (infos != null && infos.Length > 0)
                    {
                        List<System.Reflection.PropertyInfo> infoList = infos.ToList();
                        for (int i = 0; i < target.DataRow.Table.Columns.Count; i++)
                        {
                            var name = target.DataRow.Table.Columns[i].ColumnName;
                            System.Reflection.PropertyInfo _info = infoList.Find(info => { return info.Name == name; });
                            if (_info != null)
                            {
                                _info.SetValue(pm, Convert.ChangeType(target.DataRow[name], _info.PropertyType));
                            }
                        }
                        pm.Update();
                        return true;
                    }

                }
            }
            return false;
        }

    }
}
